<?php
/**
* @version		$Id: survey.php 10752 2008-08-23 01:53:31Z eddieajau $
* @package		Joomla
* @subpackage	Surveys
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
* @package		Joomla
* @subpackage	Surveys
*/
class SurveyModelSurvey extends JModel
{
	/**
	 * Add vote
	 * @param int The id of the survey
	 * @param int The id of the option selected
	 */
	function vote( $survey_id, $vote_id, $startId, $endId, $dataValue )
	{
		$db = $this->getDBO();
		if ($vote_id == 0){
			$query = 'SELECT MAX(vote_id) as maxid FROM jos_survey_date ';
			$db->setQuery( $query );
			$maxes = $db->loadObjectList();
			if(!count( $maxes ) || !$maxes[0]->maxid)
				$vote_id = 1;
			else
				$vote_id = $maxes[0]->maxid + 1;
		}
		
		$date =& JFactory::getDate();
		for($i = $startId; $i <= $endId; $i++){
			$query = 'INSERT INTO #__survey_date'
				. ' SET date = ' . $db->Quote($date->toMySQL())
				. ', vote_id = ' . (int) $vote_id
				. ', survey_id = ' . (int) $survey_id
				. ', data_id = ' . (int) $i
				. ', val = \'' . $dataValue[$i] . '\''
				;
			$db->setQuery( $query );
			$db->query();
		}
		return $vote_id;
	}
}
